<template>
	<view>
		<view class="relly_top">
			<view class="relly_top_t">
				<u-avatar :src="newDatas.selfInfo.images"></u-avatar>
				<view class="relly_score">
					{{newDatas.selfInfo.username }}<text class="relly_text">{{newDatas.selfInfo.score}}</text> 分
				</view>
			</view>
			<view class="relly_top_b">
				<u-avatar :src="newDatas.opponentInfo.images"></u-avatar>
				<view class="relly_score">
					{{newDatas.opponentInfo.username }} <text class="relly_text">{{newDatas.opponentInfo.score}}</text>
					分
				</view>
			</view>
		</view>

		<view class="relly_botton">
			<view>
				<pxingExam :nownum='nownum' :examlist='testlist' @getEveryAnswer='getEveryAnswer'
					@getLastAnswer='getLastAnswer'></pxingExam>
			</view>
		</view>

	</view>
</template>

<script>
	import pxingExam from '@/components/pxing-exam/pxing-exam2.vue';
	export default {
		components: {
			pxingExam
		},
		onBackPress(e) {
			console.log(e);
			if (e.from == 'backbutton') {
				uni.showModal({
					title: '提示',
					content: '当你退出自动失败？',
					success: function(res) {
						if (res.confirm) {
							uni.navigateBack({
								delta: 2
							});
						} else if (res.cancel) {
							uni.showToast({
								title: '用户点击取消',
								duration: 1000
							})
						}

					}
				});
				return true; //阻止默认返回行为
			}
		},
		data() {
			return {
				src: 'https://cdn.uviewui.com/uview/album/6.jpg',
				text: '无头像',
				testlist: {},
				nownum: 1,
				score: 0,
				score2: 0,
				userid: 2,
				newData: [],
				newDatas: {}
			}
		},
		onLoad(params) {
			this.newDatas = JSON.parse(decodeURIComponent(params.data))

			this.testlist = this.newDatas.questions;

		},
		watch: {
			getMessgae(newData, oldData) {

				this.newDatas.opponentInfo.score = newData.chatMessage.data.score

				if (this.newDatas.opponentInfo.score == 20) {

					this.redirtThing(2)
				}
			}
		},
		computed: {
			getMessgae() {
				return this.$store.state.websocketData;
			},
		},
		methods: {


			getEveryAnswer(e, m) {

				//证明用户等分
				if (m != undefined) {
					this.newDatas.selfInfo.score += 5
				}

				//在这里得到第二道题目
				this.nownum++;
				if (this.newDatas.selfInfo.score == 20) {
					this.redirtThing(1)
				}
				let flag = m == undefined ? 0 : 1
				uni.$u.http.post('/app/paper/getRankQuestion', {
					"id": this.nownum,
					"userAnswerId": e,
					"flag": flag,
					"uuid": this.newDatas.uuid
				}).then(
					(res) => {
						this.testlist = res.data
						var chatMessage = {};
						var sender = this.userid;
						var data = this.newDatas.selfInfo.score;
						var type = "PLAY_GAME";
						chatMessage.sender = sender;
						chatMessage.data = data;
						chatMessage.type = type;
						console.log("用户:" + sender + "更新分数为" + data);
						this.sendMessgae(chatMessage)
					}
				)
			},
			playgames() {

			},
			sendMessgae(chatMessage) {
				this.$store.dispatch('websocketSend', chatMessage)
			},
			redirtThing(type) {

				var chatMessage = {};
				if (type == 1) {
					var sender = this.newDatas.selfInfo.userId;
				} else {
					var sender = this.newDatas.opponentInfo.userId;
				}
				var type = "GAME_OVER";
				chatMessage.sender = sender;
				chatMessage.type = type;
				console.log("用户:" + sender + "结束游戏");
				this.sendMessgae(chatMessage)
				let data = {
					myuserInfo: this.newDatas.selfInfo,
					otherInfo: this.newDatas.opponentInfo,
					uuid: this.newDatas.uuid
				}
				uni.redirectTo({
					url: '../success/success?data=' + encodeURIComponent(JSON.stringify(data)),
					animationType: 'pop-in',
					animationDuration: 2000
				})
			}


		}
	}
</script>

<style scoped lang="scss">
	page {
		// background-color: #4E75FA;
	}

	.relly_top {
		height: 250rpx;
		display: flex;
		flex-direction: row;
		margin: 50rpx;

		.relly_top_t {
			flex: 1;
			margin: 5rpx;
			background-color: #F54F75;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			flex-direction: column;
			padding-top: 40rpx;

			.relly_score {
				color: #FFFFFF;

				.relly_text {
					font-size: 40rpx;
					margin-left: 20rpx;
				}
			}
		}

		.relly_top_b {
			flex: 1;
			margin: 5rpx;
			background-color: #3F3EB5;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			flex-direction: column;
			padding-top: 40rpx;

			.relly_score {
				color: #FFFFFF;

				.relly_text {
					font-size: 40rpx;
					margin-left: 20rpx;
				}
			}


		}
	}
</style>
